package com.hetongxue.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.UUID;

/**
 * @Description: 文件工具类
 * @ClassNmae: FileUtils
 * @Author: 何同学
 * @DateTime: 2022-03-21 23:07
 **/
@Slf4j
public class FileUtil {


    // 限制5M大小
    private static final long FILESIZEMAX = 5242880;
    // 设置上传位置
//    private static final String UPLOADPATH = "src/main/resources/upload";
    private static final String UPLOADPATH = "reagent-web/src/main/resources/upload";

    // 单文件上传
    public static String FileUploadOne(MultipartFile file) throws Exception {
        // 初始化保存地址
        String savePath;
        // 获取服务器地址
        String serverPath = HttpUtil.getServerPath();
        // 判断文件是否为空,为空时使用默认图片进行上传
        if (file.isEmpty()) {
            savePath = serverPath + "upload/default/default.png";
            log.warn("没有图片存在,已使用默认图片上传!");
            log.info("保存地址:" + savePath);
            return serverPath;
        }
        // 判断文件大小(可忽略)
        if (file.getSize() > FILESIZEMAX) {
            throw new Exception("文件过大");
        }
        // 获取文件扩展
        String extension = "." + FilenameUtils.getExtension(file.getOriginalFilename());
        // 创建新的文件名
        String newFileName =
                new SimpleDateFormat("yyyyMMssHHmmss").format(new Date()) + UUID.randomUUID().toString().replace("-", "") + extension;
        // 获取上传文件夹路径
        String classPath = ResourceUtils.getURL("").getPath() + UPLOADPATH;
        // 新建日期文件夹分类
        String dateDir = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        // 最终上传地址
        String uploadPath = classPath + "/" + dateDir;
        // 判断文件夹是否存在
        File uploadPathFile = new File(uploadPath);
        if (!uploadPathFile.exists())
            uploadPathFile.mkdirs();
        // 开始上传
        log.info("开始上传...");
        file.transferTo(new File(uploadPathFile, newFileName));
        savePath = serverPath + "upload/" + dateDir + "/" + newFileName;
        log.info("上传地址：" + uploadPath);
        log.info("保存地址：" + savePath);
        return savePath;
    }

    // 文件批量上传
    public static String[] FileUploadBatch(LinkedList<MultipartFile> files) throws Exception {
        if (files.size() == 0) return null;
        String[] savePath = new String[files.size()];
        int index = 0;
        String serverPath = HttpUtil.getServerPath();
        for (MultipartFile file : files) {
            if (file.isEmpty()) {
                savePath[index] = serverPath + "upload/default/default.png";
                index++;
                log.warn("第" + index + "个文件中没有图片存在,已使用默认图片上传!");
                log.info("(" + index + ")保存地址:" + savePath);
                continue;
            }
            String newFileName =
                    new SimpleDateFormat("yyyyMMssHHmmss")
                            .format(new Date()) + UUID.randomUUID().toString()
                            .replace("-", "") + "." + FilenameUtils
                            .getExtension(file.getOriginalFilename());
            String classPath = ResourceUtils.getURL("").getPath() + UPLOADPATH;
            String dateDir = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            String uploadPath = classPath + "/" + dateDir;
            File uploadPathFile = new File(uploadPath);
            if (!uploadPathFile.exists())
                uploadPathFile.mkdirs();
            // 开始上传
            log.info("开始上传第" + index + 1 + "个图片文件...");
            file.transferTo(new File(uploadPathFile, newFileName));
            savePath[index] = serverPath + "upload/" + dateDir + "/" + newFileName;
            index++;
            log.info("(" + index + ")上传地址：" + uploadPath);
            log.info("(" + index + ")保存地址：" + savePath[index - 1]);
        }
        return savePath;

    }

}
